Online Class

Online Class
Author

Khushi

Introduction

We are looking at Geospatial data and how to use it with intent.

knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(tmap)
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')
library(osmdata)
Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(rnaturalearth)

Specifying an Area of Interest

# https://boundingbox.klokantech.com
# CSV: 77.574028,12.917262,77.595073,12.939895
bbox_1 <- matrix(
  c(77.574028, 12.917262, 77.595073, 12.939895),
  byrow = FALSE,
  nrow = 2,
  ncol = 2,
  dimnames = list(c('x', 'y'), c('min', 'max'))
)
bbox_1
       min      max
x 77.57403 77.59507
y 12.91726 12.93989

This specifies a bounding box for an area of interest. The bounding box is represented as a matrix named bbox_1, which stores the minimum and maximum x and y coordinates. The x-values (longitude) range from 77.57403 to 77.59507, and the y-values (latitude) range from 12.91726 to 12.93990. The matrix has two rows and two columns, and the coordinate labels (‘x’, ‘y’) are set as the row names, while ‘min’ and ‘max’ are set as the column names.

Using the getbb command from the osmdata package

bbox_2 <- osmdata::getbb("Jayanagar, Bangalore, India")
bbox_2
       min      max
x 77.58528 77.58929
y 12.93650 12.94369

This retrieves the bounding box for Jayanagar, Bangalore, India, using the osmdata::getbb function. The resulting bounding box matrix, bbox_2, specifies the minimum and maximum x and y coordinates. The x-values range from 77.58528 to 77.58929, while the y-values range from 12.93650 to 12.94369.

Output of the bbox_1 matrix

bbox_1
       min      max
x 77.57403 77.59507
y 12.91726 12.93989

This gives the output of the bbox_1 matrix, which specifies a bounding box for an area of interest. The matrix consists of minimum and maximum x and y coordinates. The x-values range from 77.57403 to 77.59507, and the y-values range from 12.91726 to 12.93989.

Output of the bbox_2 matrix

bbox_2
       min      max
x 77.58528 77.58929
y 12.93650 12.94369

The image shows the output of the bbox_2 matrix, which defines a bounding box for a specific area. The matrix contains the minimum and maximum x and y coordinates. The x-values range from 77.58528 to 77.58929, and the y-values range from 12.93650 to 12.94369.

Available Features

osmdata::available_features() %>% as_tibble()
# A tibble: 272 × 1
   value                  
   <chr>                  
 1 4wd_only               
 2 abandoned              
 3 abutters               
 4 access                 
 5 addr                   
 6 addr:city              
 7 addr:conscriptionnumber
 8 addr:country           
 9 addr:county            
10 addr:district          
# ℹ 262 more rows

The table shows a list of available features in the OpenStreetMap (OSM) dataset, converted into a tibble with 272 rows. The features displayed in the output include elements like “4wd_only,” “abandoned,” “abutters,” “access,” and various address-related tags. These represent different categories or attributes that can be used when extracting or analyzing geographical data from OSM, indicating the variety of metadata available for locations.

Available Tags - Highway

available_tags(feature = "highway")
# A tibble: 56 × 2
   Key     Value              
   <chr>   <chr>              
 1 highway bridleway          
 2 highway bus_guideway       
 3 highway bus_stop           
 4 highway busway             
 5 highway construction       
 6 highway corridor           
 7 highway crossing           
 8 highway cycleway           
 9 highway cyclist_waiting_aid
10 highway elevator           
# ℹ 46 more rows

The output represents a tibble of available tags for the “highway” feature, providing a list of 56 key-value pairs. Each key is labeled as “highway,” and the values describe specific highway-related features like “bridleway,” “bus_stop,” “busway,” “cycleway,” and “crossing.” These tags offer various classifications for highways and related infrastructure, helping to map different types of roads and pathways.

Available Tags - Amenity

available_tags("amenity")
# A tibble: 137 × 2
   Key     Value          
   <chr>   <chr>          
 1 amenity animal_boarding
 2 amenity animal_breeding
 3 amenity animal_shelter 
 4 amenity animal_training
 5 amenity arts_centre    
 6 amenity atm            
 7 amenity baby_hatch     
 8 amenity baking_oven    
 9 amenity bank           
10 amenity bar            
# ℹ 127 more rows

The table shows the available tags for the “amenity” feature, providing a list of 137 key-value pairs. Each key is labeled as “amenity,” and the values describe specific amenity-related features like “animal_boarding,” “animal_shelter,” “arts_centre,” “atm,” “bank,” and “bar.” These tags classify various public or private facilities and services in an area, aiding in mapping and analysis of amenities.

Available Tags - Natural

available_tags("natural")
# A tibble: 49 × 2
   Key     Value        
   <chr>   <chr>        
 1 natural arch         
 2 natural arete        
 3 natural bare_rock    
 4 natural bay          
 5 natural beach        
 6 natural blockfield   
 7 natural blowhole     
 8 natural cape         
 9 natural cave_entrance
10 natural cliff        
# ℹ 39 more rows

The output shows the available tags for the “natural” feature, listing 49 key-value pairs. Each key is labeled “natural,” and the values represent various natural features such as “arch,” “arete,” “bay,” “beach,” “cliff,” “blowhole,” “cape,” and “cave_entrance.” These tags are used to categorize different natural elements in a geographic area, aiding in mapping and analyzing natural environments.

Features within bbox_2

# Get all restaurants, atms, colleges within my bbox
locations <- 
  osmdata::opq(bbox = bbox_2) %>% 
  osmdata::add_osm_feature(key = "amenity", 
                           value = c("restaurant", "atm", "college")) %>% 
  osmdata_sf() %>%  # Convert to Simple Features format
  purrr::pluck("osm_points") # Pull out the data frame of interest

# Get all buildings within my bbox
dat_buildings <-
  osmdata::opq(bbox = bbox_2) %>% 
  osmdata::add_osm_feature(key = "building") %>% 
  osmdata_sf() %>% 
  purrr::pluck("osm_polygons") 

# Get all residential roads within my bbox
dat_roads <- 
  osmdata::opq(bbox = bbox_2) %>% 
  osmdata::add_osm_feature(key = "highway", 
                           value = c("residential")) %>% 
  osmdata_sf() %>% 
  purrr::pluck("osm_lines") 

# Get all parks / natural /greenery areas and spots within my bbox
dat_natural <-   
  osmdata::opq(bbox = bbox_2) %>% 
  osmdata::add_osm_feature(key = "natural",
                           value = c("tree", "water", "wood")) %>% 
  osmdata_sf()
dat_natural
Object of class 'osmdata' with:
                 $bbox : 12.9364951,77.5852775,12.9436891,77.5892901
        $overpass_call : The call submitted to the overpass API
                 $meta : metadata including timestamp and version numbers
           $osm_points : 'sf' Simple Features Collection with 0 points
            $osm_lines : NULL
         $osm_polygons : 'sf' Simple Features Collection with 0 polygons
       $osm_multilines : NULL
    $osm_multipolygons : NULL
dat_trees <- 
  dat_natural %>% 
  purrr::pluck("osm_points") 

dat_greenery <- 
  dat_natural %>% pluck("osm_polygons")

This code extracts various features from OpenStreetMap (OSM) data within a specified bounding box (bbox_2). The first part of the code retrieves restaurants, ATMs, and colleges using the “amenity” tag, converting the result into simple features and extracting points of interest. The second section gathers building data by filtering for the “building” tag and extracting polygons. The third part retrieves residential roads using the “highway” tag, extracting line data. Lastly, the code fetches parks and natural areas such as trees, water, and wood using the “natural” tag, extracting both points (for trees) and polygons (for greenery). The second image provides metadata information from the OSM data, including the bounding box coordinates, the Overpass API call, and the presence of points and polygons with 0 features for the extracted dataset.

Writing spatial data to GeoPackage files

st_write(dat_roads, dsn = "roads.gpkg", 
         append = FALSE, quiet = FALSE)
Deleting layer `roads' using driver `GPKG'
Writing layer `roads' to data source `roads.gpkg' using driver `GPKG'
Writing 39 features with 8 fields and geometry type Line String.
st_write(dat_buildings, 
         dsn = "buildings.gpkg", 
         append = FALSE, 
         quiet = FALSE)
Deleting layer `buildings' using driver `GPKG'
Writing layer `buildings' to data source `buildings.gpkg' using driver `GPKG'
Writing 551 features with 9 fields and geometry type Polygon.
st_write(dat_greenery, dsn = "greenery.gpkg", 
         append = FALSE,quiet = FALSE)
Deleting layer `greenery' using driver `GPKG'
Writing layer `greenery' to data source `greenery.gpkg' using driver `GPKG'
Writing 0 features with 1 fields and geometry type Polygon.
st_write(dat_trees, dsn = "trees.gpkg", 
         append = FALSE,quiet = FALSE)
Deleting layer `trees' using driver `GPKG'
Writing layer `trees' to data source `trees.gpkg' using driver `GPKG'
Writing 0 features with 1 fields and geometry type Point.

The dat_roads object, containing 39 features with 8 fields of Line String geometry, is written to roads.gpkg. The dat_buildings object, with 551 features and 9 fields of Polygon geometry, is written to buildings.gpkg. The dat_greenery and dat_trees objects are also written to GeoPackage files (greenery.gpkg and trees.gpkg), but both contain 0 features with 1 field for Polygon and Point geometries, respectively.

Reading the buildings.gpkg file

buildings <- st_read("./buildings.gpkg")
Reading layer `buildings' from data source 
  `C:\Srishti\Year 2\Data as Material\R\posts\Online Class\buildings.gpkg' 
  using driver `GPKG'
Simple feature collection with 551 features and 9 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 77.58523 ymin: 12.9361 xmax: 77.58935 ymax: 12.94357
Geodetic CRS:  WGS 84

The output shows the process of reading the buildings.gpkg file using the st_read function. It reads the layer named ‘buildings’ from the specified file path, which contains 551 features and 9 fields. The geometry type is POLYGON, and the data uses the XY dimension. The bounding box of the data spans from xmin: 77.58523, ymin: 12.9361 to xmax: 77.58935, ymax: 12.94357. The file uses the WGS 84 geodetic coordinate reference system (CRS).

Reading the greenery.gpkg file

greenery <- st_read("./greenery.gpkg")
Reading layer `greenery' from data source 
  `C:\Srishti\Year 2\Data as Material\R\posts\Online Class\greenery.gpkg' 
  using driver `GPKG'
Simple feature collection with 0 features and 1 field
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84

This reads the greenery.gpkg file using the st_read function. It reads the layer named ‘greenery’ from the specified file path, which contains 0 features and 1 field. The bounding box values (xmin, ymin, xmax, ymax) are all NA, indicating there are no spatial features in this layer.

Reading the trees.gpkg file

trees <- st_read("./trees.gpkg")
Reading layer `trees' from data source 
  `C:\Srishti\Year 2\Data as Material\R\posts\Online Class\trees.gpkg' 
  using driver `GPKG'
Simple feature collection with 0 features and 1 field
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84

This code reads the layer named ‘trees’ from the specified file path, which contains 0 features and 1 field. The bounding box values (xmin, ymin, xmax, ymax) are all NA, indicating no spatial data is present in this layer.

Reading the roads.gpkg file

roads <- st_read("./roads.gpkg")
Reading layer `roads' from data source 
  `C:\Srishti\Year 2\Data as Material\R\posts\Online Class\roads.gpkg' 
  using driver `GPKG'
Simple feature collection with 39 features and 8 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 77.58511 ymin: 12.936 xmax: 77.59122 ymax: 12.94625
Geodetic CRS:  WGS 84

This demonstrates reading the roads.gpkg file using the st_read function. It accesses the ‘roads’ layer from the given file path, containing 39 features and 8 attributes. The geometry type for these features is LINESTRING, with data represented in the XY dimension. The bounding box coordinates range from xmin: 77.58511, ymin: 12.936 to xmax: 77.59122, ymax: 12.94625.

How many buildings?

nrow(buildings)
[1] 551

The code returns the number of rows in the buildings dataset. The dataset contains 551 buildings.

Geometry Set

buildings$geom
Geometry set for 551 features 
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 77.58523 ymin: 12.9361 xmax: 77.58935 ymax: 12.94357
Geodetic CRS:  WGS 84
First 5 geometries:
POLYGON ((77.58572 12.93973, 77.58536 12.93973,...
POLYGON ((77.58528 12.93943, 77.58563 12.93943,...
POLYGON ((77.58545 12.93994, 77.58533 12.93994,...
POLYGON ((77.58531 12.94194, 77.5853 12.94186, ...
POLYGON ((77.58802 12.93672, 77.58809 12.93674,...

The output displays a geometry set for 551 features, where the geometry type is a polygon with XY dimensions. The bounding box of the data covers a range of coordinates between xmin: 77.58523, ymin: 12.9361 and xmax: 77.58935, ymax: 12.94357, following the WGS 84 coordinate reference system (CRS). The first five geometries are shown as sets of latitude and longitude coordinates that define the polygons representing buildings or structures. These polygons outline the spatial extent of the buildings in the dataset.

Class of the buildings$geom object

class(buildings$geom)
[1] "sfc_POLYGON" "sfc"        

The class of the buildings$geom object is sfc_POLYGON and sfc, which indicates that this object contains a collection of simple feature geometries, specifically polygons. These polygons represent the spatial boundaries or shapes of features (such as buildings) in a geographic dataset, with the sfc class standing for simple feature collection.

My first Map in R